<?php
	include_once "config_db.php";
	
	$database = 'mymap';
	
	if (!mysql_connect($db_host, $db_user, $db_pwd))
	die("Can't connect to database");
	
	if (!mysql_select_db($database))
	die("Can't select database");
	
	mysql_query("SET NAMES 'utf8'");
	
	function getType_Administrative($locality_level) {
	
		$table = 'administrative_map';
		$result = mysql_query("SELECT id, name
								FROM {$table} where locality_level = ".$locality_level);
		return $result;
	}
	function getSegment() {
	
		$table = 'segment';
		$result = mysql_query("SELECT id, start_node, end_node
								FROM {$table}");
		return $result;
	}
	function getSegmentByID($segment_id) {
	
		$table = 'segment';
		$result = mysql_query("SELECT id, start_node, end_node
								FROM {$table}
								WHERE id = ".$segment_id);
		return $result;
	}
	function getSegmentByStartNode($start_node_id) {
	
		$table = 'segment';
		$result = mysql_query("SELECT id, start_node, end_node
								FROM {$table}
								WHERE start_node = ".$start_node_id);
		return $result;
	}
	function getSegmentTagByID($segment_id) {
		$table = 'segment_tag';
		$result = mysql_query("SELECT id, k, value
								FROM {$table}
								WHERE segment_id = ".$segment_id);
		return $result;
	}
	function getSegmentDetailByID($segment_id) {
		$table = 'segment_nd';
		$result = mysql_query("SELECT nd
								FROM {$table}
								WHERE segment_id = ".$segment_id);
		return $result;
	}
	function getNodeBydID($node_id) {
	
		$table = 'node';
		$result = mysql_query("SELECT id, lat, lng
								FROM {$table}
								WHERE id = ".$node_id);
		
		return $result;
	}	
	function getAllNode() {
	
		$table = 'node';
		$result = mysql_query("SELECT id
								FROM {$table}");
		
		return $result;
	}
	
	function getNode() {
	
		$table = 'node';
		$result = mysql_query("SELECT id
							   FROM {$table}
							   WHERE nodeTo != 'NULL'");
		
		return $result;
	}
	
	function getLastID($table_input) {
		
		$result = mysql_query("SELECT id FROM {$table_input}");
		$id = null;
		if(mysql_num_rows($result) == 0) {
			$id = 1;
		} else {
			while($data = mysql_fetch_array($result)) {
				$id = $data["id"];
			}	
		}
		return $id;
	}
?>